<?php
$sub_menu = "400100";
include_once("./_common.php");
include_once("$g4[path]/lib/cheditor4.lib.php");

auth_check($auth[$sub_menu], "w");
$g4[title] = "상품 등록";
include_once("./admin.head.php");

$GOODS_DIR = $g4[path]."/data/goods/";

sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_qty` INT( 5 ) NOT NULL DEFAULT '1' AFTER `g_name`  ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_show` CHAR( 1 ) NOT NULL DEFAULT '1' AFTER `g_qty` ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_order_flag` CHAR( 1 ) NOT NULL DEFAULT '1' AFTER `g_show` ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_order` INT( 6 ) NOT NULL DEFAULT '0' AFTER `g_order_flag` ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_icon` varchar( 50 ) NOT NULL AFTER `g_order` ", false);
sql_query(" ALTER TABLE `" . $g4['goods_table'] . "` ADD `g_sell_price` int( 10 ) NOT NULL AFTER `g_info` ", false);


if($w == "UPDATE" && $IDX!="")
{
	$SQL = "SELECT *
						FROM " . $g4['goods_table'] . "
							WHERE idx='" . $IDX . "'
								LIMIT 1";

	$DATA					= sql_fetch($SQL);
	$g_code				= $DATA[g_code];
	$g_sell_price	= $DATA['g_sell_price'];
	$g_price			= $DATA[g_price];
	$g_tranc			= $DATA[g_tranc];
	$g_cnt				= $DATA[g_cnt];
	$g_min				= $DATA[g_min];
	$g_max				= $DATA[g_max];
	$g_qty				= $DATA['g_qty'];
	$g_show				= $DATA['g_show'];
	$g_order_flag   = $DATA['g_order_flag'];
	$g_order				= $DATA['g_order'];
	$g_icon					= $DATA['g_icon'];

}
else
{
	$g_code = "G".time().rand(111,999);
	$g_price = 0;
	$g_tranc = "Y";
	$g_sell_price	= 0;
	$g_cnt = 100;
	$g_min = 0;
	$g_max = 0;
	$g_qty = 1;
	$g_show   = 1;
	$g_order_flag   = 1;
	$g_order   = 0;
}

?>


<script src="<?=$g4[cheditor4_path]?>/cheditor.js"></script>
<?=cheditor1('g_content', '100%', '250');?>

<table cellpadding="2" cellspacing="1" border="0" width="700">
<form name="goods_frm" method="post" enctype="multipart/form-data" action="goods_update.php" onsubmit="CheckFrm();return false;">
<input type="hidden" name="w" value="<?=$w?>">
<input type="hidden" name="IDX" value="<?=$IDX?>">
	<colgroup>
		<col width="120">
		<col width="">
	</colgroup>
	<tr><td colspan="2" height="2" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">카테고리</td>
		<td>
		<select name="c_code">
			<option value="">선택</option>
			<?
			$cate_sql = "select * from g4_category order by c_pcode asc,c_depth asc,c_sort asc";
			$cate_rst = mysql_query($cate_sql);
			while($cate_row = mysql_fetch_array($cate_rst))
			{
			?>
			<option value="<?=$cate_row[c_code]?>" style="<?if($cate_row[c_depth]){echo("background:#CCCCFF;");}else{echo("background:#0000FF;color:#FFFFFF");}?>" <?if($DATA[c_code]==$cate_row[c_code]){echo("selected");}?>><?if($cate_row[c_depth]){echo("&nbsp;&nbsp;&nbsp;");}?><?=$cate_row[c_name]?></option>
			<?}?>
		</select>
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">상품코드</td>
		<td><input type="text" name="g_code" class="ed" style="width:120px;background:#EFEFEF" readonly value="<?=$g_code?>"></td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">상품명</td>
		<td><input type="text" name="g_name" class="ed" style="width:500px" value="<?=$DATA[g_name]?>"></td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">판매단위개수</td>
		<td>
			<input type="text" id="g_qty" name="g_qty" value="<?=$g_qty?>" style="width:30px;" /> 개
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">진열여부</td>
		<td>
			<select name="g_show">
				<option value="1" <?php if($g_show == '1') echo " selected='selected' "; ?> >진열함</option>
				<option value="2" <?php if($g_show == '2') echo " selected='selected' "; ?> >진열하지 않음</option>
			</select>
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">판매여부</td>
		<td>
			<select name="g_order_flag">
				<option value="1" <?php if($g_order_flag == '1') echo " selected='selected' "; ?> >주문가능</option>
				<option value="2" <?php if($g_order_flag == '2') echo " selected='selected' "; ?> >주문불가능</option>
			</select>
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">아이콘</td>
		<td>
			<?php
				if(!empty($g_icon))
				{
					$g_icon_arr = explode("^^", $g_icon);
				}
			?>
			<input type="checkbox" name="g_icon1" value="1" <?php if($g_icon_arr[0] == "1") echo " checked='checked' ";?> />신규상품
			<input type="checkbox" name="g_icon2" value="1" <?php if($g_icon_arr[1] == "1") echo " checked='checked' ";?> />추천상품
			<input type="checkbox" name="g_icon3" value="1" <?php if($g_icon_arr[2] == "1") echo " checked='checked' ";?> />행사상품
			<input type="checkbox" name="g_icon4" value="1" <?php if($g_icon_arr[3] == "1") echo " checked='checked' ";?> />세일상품
		</td>
	</tr>

	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">홍보문구</td>
		<td><textarea name="g_info" class="ed" style="width:100%;height:60px;overflow:auto"><?=$DATA[g_info]?></textarea></td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">제조/판매원</td>
		<td><input type="text" name="g_create" class="ed" style="width:300px" value="<?=$DATA[g_create]?>"></td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">원산지</td>
		<td><input type="text" name="g_nation" class="ed" style="width:300px" value="<?=$DATA[g_nation]?>"></td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">판매가</td>
		<td><input type="text" name="g_sell_price" class="ed" style="width:120px;text-align:right" value="<?=$g_sell_price?>">원</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">할인가</td>
		<td><input type="text" name="g_price" class="ed" style="width:120px;text-align:right" value="<?=$g_price?>">원</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">재고</td>
		<td><input type="text" name="g_cnt" class="ed" style="width:80px;text-align:right" value="<?=$g_cnt?>"></td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">최소구매수량</td>
		<td><input type="text" name="g_min" class="ed" style="width:80px;text-align:right" value="<?=$g_min?>"> (0은 제한 없음)</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">최대구매수량</td>
		<td><input type="text" name="g_max" class="ed" style="width:80px;text-align:right" value="<?=$g_max?>"> (0은 제한 없음)</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">옵션<span style="font-weight:bold;cursor:pointer" onclick="AddRows()">+</span></td>
		<td>
			<table cellpadding="0" cellspacing="1" border="0" id="optionTable" width="100%">
				<tr align="center" bgcolor="#EFEFEF">
					<td width="">옵션명</td>
					<td width="120">옵션가격</td>
					<td width="50">삭제</td>
				</tr>
				<?
				if($w == "UPDATE")
				{
					$g_option = explode("[RECORD]",$DATA[g_option]);
					if($DATA[g_option]!="")
					{
						for($i=0;$i<count($g_option);$i++)
						{
							$tmp_option = explode("[FIELD]",$g_option[$i]);
							?>
				<tr>
					<td><input type="text" class="ed" style="width:100%" name="g_option_name[]" value="<?=$tmp_option[0]?>"></td>
					<td><input type="text" class="ed" style="width:100px;text-align:right" name="g_option_price[]" value="<?=$tmp_option[1]?>">원</td>
					<td><button class='btn1' onclick='SubRows(this)'>취소</button></td>
				</tr>
							<?
						}
					}
					else
					{
					?>
				<tr>
					<td><input type="text" class="ed" style="width:100%" name="g_option_name[]"></td>
					<td><input type="text" class="ed" style="width:100px;text-align:right" name="g_option_price[]" value="0">원</td>
					<td></td>
				</tr>
					<?
					}
				}
				else
				{
				?>
				<tr>
					<td><input type="text" class="ed" style="width:100%" name="g_option_name[]"></td>
					<td><input type="text" class="ed" style="width:100px;text-align:right" name="g_option_price[]" value="0">원</td>
					<td></td>
				</tr>
				<?}?>
			</table>
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr>
		<td colspan="2"><?=cheditor2('g_content', stripslashes($DATA[g_content]));?></td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">
		이미지(소)
		<br><span style="font-size:11px;font-weight:normal;color:#666666">100px X 100px</span>
		</td>
		<td>
		<input type="file" name="g_img1" class="ed" style="width:300px">
		<?
		if($DATA[g_img1]!="")
		{
			$IMG_SIZE = getimagesize($GOODS_DIR.$DATA[g_img1]);
		?>
		<a href="javascript:ShowImg('<?=$GOODS_DIR.$DATA[g_img1]?>','<?=$IMG_SIZE[0]?>','<?=$IMG_SIZE[1]?>')">미리보기</a>
		<input type="checkbox" name="g_img1_del" value="YES">삭제
		<?}?>
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">
		이미지(중)
		<br><span style="font-size:11px;font-weight:normal;color:#666666">300px X 300px</span>
		</td>
		<td>
		<input type="file" name="g_img2" class="ed" style="width:300px">
		<?
		if($DATA[g_img2]!="")
		{
			$IMG_SIZE = getimagesize($GOODS_DIR.$DATA[g_img2]);
		?>
		<a href="javascript:ShowImg('<?=$GOODS_DIR.$DATA[g_img2]?>','<?=$IMG_SIZE[0]?>','<?=$IMG_SIZE[1]?>')">미리보기</a>
		<input type="checkbox" name="g_img2_del" value="YES">삭제
		<?}?>
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">
		이미지(대)
		<br><span style="font-size:11px;font-weight:normal;color:#666666">500px X 500px</span>
		</td>
		<td>
		<input type="file" name="g_img3" class="ed" style="width:300px">
		<?
		if($DATA[g_img3]!="")
		{
			$IMG_SIZE = getimagesize($GOODS_DIR.$DATA[g_img3]);
		?>
		<a href="javascript:ShowImg('<?=$GOODS_DIR.$DATA[g_img3]?>','<?=$IMG_SIZE[0]?>','<?=$IMG_SIZE[1]?>')">미리보기</a>
		<input type="checkbox" name="g_img3_del" value="YES">삭제
		<?}?>
		<br><input type="checkbox" name="autosize" value="Y" onclick="AutoReSize(this);">이미지(소),이미지(중) 자동등록
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">화면표시순서</td>
		<td>
			<input type="text" class="ed" style="width:60px" name="g_order" value="<?=$g_order?>" />
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr height="25">
		<td class="bgcol1 bold">배송설정</td>
		<td>
		<input type="radio" name="g_tranc" value="Y" <?if($g_tranc == "Y"){echo("checked");}?>>유료
		<input type="radio" name="g_tranc" value="N" <?if($g_tranc == "N"){echo("checked");}?>>무료
		<input type="radio" name="g_tranc" value="C" <?if($g_tranc == "C"){echo("checked");}?>>착불
		</td>
	</tr>
	<tr><td colspan="2" height="1" bgcolor="#D7D7D7"></td></tr>
	<tr>
		<td colspan="2" height="40" align="center">
		<button class="btn1" style="width:60px" onclick="CheckFrm();">등록</button>&nbsp;
		<button class="btn1" style="width:60px" onclick="history.back(-1);">취소</button>
		</td>
	</tr>
</form>
</table>
<script>
function AutoReSize(Obj)
{
	if(Obj.checked == true)
	{
		document.all('g_img1').disabled = true;
		document.all('g_img2').disabled = true;
	}
	else
	{
		document.all('g_img1').disabled = false;
		document.all('g_img2').disabled = false;
	}
}

function CheckFrm()
{
	var frm = document.goods_frm;

	if(frm.c_code.value == "")
	{
		alert("카테고리를 선택하세요.");
		frm.c_code.focus();
	}
	else if(frm.g_name.value == "")
	{
		alert("상품명을 입력하세요.");
		frm.g_name.focus();
	}
	else
	{
		if(confirm("등록 하시겠습니까?"))
		{
			<?=cheditor3('g_content')?>
			frm.submit();
		}
		else
		{
			return false;
		}
	}
}

function ShowImg(IMG,IMG_X,IMG_Y)
{
	var URL = "";
	var NAME = "";
	var OPTION = "left=0,top=0,width="+IMG_X+",height="+IMG_Y+",resizable=no,scrollbars=no";

	var HTML = "<img src='"+IMG+"' style='cursor:pointer' onclick='self.close();'>";

	var OpenImg = window.open(URL,NAME,OPTION);
	OpenImg.document.title = "미리보기";
	OpenImg.document.body.style.margin = "0px";
	OpenImg.document.body.innerHTML = HTML;
}


function AddRows()
{
	var TARGET = "optionTable";
	var objTbl;
	var objRow;
	var objCell;
	if (document.getElementById)
	{
		objTbl = document.getElementById(TARGET);
	}
	else
	{
		objTbl = document.all[TARGET];
	}

	var NewRow = objTbl.rows.length;

	objRow = objTbl.insertRow(NewRow);
	objRow.id = NewRow;
	objCell1 = objRow.insertCell(0);
	objCell1.innerHTML = "<input type=\"text\" class=\"ed\" style=\"width:100%\" name=\"g_option_name[]\">";
	objCell2 = objRow.insertCell(1);
	objCell2.innerHTML = "<input type=\"text\" class=\"ed\" style=\"width:100px;text-align:right\" name=\"g_option_price[]\" value=\"0\">원";
	objCell3 = objRow.insertCell(2);
	objCell3.innerHTML = "<button class='btn1' onclick='SubRows(this)'>취소</button>";
}

function SubRows(btn)
{
	var TARGET = "optionTable";
	var objTbl = document.all(TARGET);
	var delRow = btn.parentNode.parentNode.rowIndex;
	objTbl.deleteRow(delRow);
}
</script>
<?
include_once ("./admin.tail.php");
?>